using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using HttpClient;

namespace FulcrumApi.clients
{
    public class HttpHelper
    {
        static CookieContainer container = new CookieContainer();

        public static HttpResponse Post(String url, Dictionary<String, String> parameters, String authenticityToken, HTTPClient client)
        {
        	return DoPost.On(url).Using.Parameters(parameters)
        		.Header("X-CSRF-Token", authenticityToken)
        		.AllowAutoRedirect(false)
        		.ContentType("application/x-www-form-urlencoded")
        		.And.InContent.Parameters(parameters)
        		.And.ThenCall(client);
        }

        public static HttpResponse Delete(String url, Dictionary<String, String> parameters, String authenticityToken, HTTPClient client)
        {
        	return DoDelete.On(url)
        		.Using.AllowAutoRedirect(false)
        		.Header("X-CSRF-Token", authenticityToken)
        		.And.InContent.Parameters(parameters)
        		.And.ThenCall(client);
        }

        public static HttpResponse PostJson(String url, String content, String authenticityToken, HTTPClient client)
        {
        	return DoPost.On(url).Using
        		.Header("X-CSRF-Token", authenticityToken)
        		.And.InContent.Json(content.Trim())
        		.And.ThenCall(client);
        }

        public static HttpResponse Get(String url, Dictionary<String, String> parameters, String authenticityToken, HTTPClient client)
        {
            return DoGet.On(url).Using.Parameters(parameters)
                .Header("X-CSRF-Token", authenticityToken)
                .Header("X-Transaction", "POST Example")
                .And.ThenCallWithClient(client);
        }

        public static HttpResponse PutJson(String url, String content, String authenticityToken, HTTPClient client)
        {
        	return DoPut.On(url).Using
        		.Header("X-CSRF-Token", authenticityToken)
        		.And.InContent.Json(content.Trim())
        		.And.ThenCall(client);
            //content = "{\"story\":{\"events\":[],\"state\":\"unscheduled\",\"story_type\":\"feature\",\"editing\":true,\"title\":\"Coucou\"}}";
//            var webReq = (HttpWebRequest)WebRequest.Create(url);
//            webReq.CookieContainer = container;
//            //webReq.AllowAutoRedirect = true; 
//            webReq.Headers.Add("X-CSRF-Token", authenticityToken);
//            var buffer = Encoding.ASCII.GetBytes(content.Trim());
//            webReq.Method = "PUT";
//            webReq.ContentType = "application/json";
//            webReq.ContentLength = buffer.Length;
//            var postData = webReq.GetRequestStream();
//            postData.Write(buffer, 0, buffer.Length);
//            postData.Close();
//            return (HttpWebResponse)webReq.GetResponse();

        }
    }
}